package com.ruoyi.web.controller.shop.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.VO.shop.ShopConfigSearchVO;
import com.ruoyi.common.entity.VO.shop.ShopConfigVO;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.web.controller.shop.service.IBackShopConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/back/shopConfig")
public class BackShopConfigController extends BaseController {

    private String prefix = "shop";

    @Resource
    private IBackShopConfigService shopConfigService;

    /**
     * 页面-店铺-编辑店铺设置
     *
     * @return
     */
    @RequiresPermissions("back:shopConfig:list")
    @GetMapping()
    public String list() {
        if (ShiroUtils.getShopId() != 0) {
            shopConfigService.refresh(ShiroUtils.getShopId(),false);
            return prefix + "/configList";
        }
        return "redirect:/back/shop/switchShop";
    }

    /**
     * 数据查询-店铺设置列表（搜索）
     *
     * @param shopConfigSearchVO
     * @return
     */
    @RequiresPermissions("back:shopConfig:list")
    @PostMapping("/searchList")
    @ResponseBody
    public TableDataInfo searchList(ShopConfigSearchVO shopConfigSearchVO) {
        startPage();
        shopConfigSearchVO.setShopId(ShiroUtils.getShopId());
        List<ShopConfigVO> list = shopConfigService.searchList(shopConfigSearchVO);
        return getDataTable(list);
    }

    /**
     * 数据更新-编辑店铺设置
     *
     * @param shopConfigVO 店铺设置信息
     * @return
     */
    @Log(title = "店铺设置管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("back:shopConfig:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ShopConfigVO shopConfigVO) {
        shopConfigVO.setShopId(ShiroUtils.getShopId());
        shopConfigVO.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        return AjaxResult.success(shopConfigService.update(shopConfigVO));
    }
}
